<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\grid\GridView;
use app\components\Wechat;
use Da\QrCode\QrCode;
use Da\QrCode\Contracts\ErrorCorrectionLevelInterface;
use Da\QrCode\Label;

$this->title = '二维码';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="qrcode-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('创建二维码', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            // ['class' => 'yii\grid\SerialColumn'],

            [
                'attribute' => 'id',
                'options' => ['width' => 20],
            ],
            [
                'attribute' => 'qrcode_img',
                'format' => 'raw',
                'value' => function($data) {
                    // $label = (new Label('新华文轩'))
                    // ->useFont(__DIR__ . '/../../vendor/2amigos/qrcode-library/resources/fonts/noto_sans.otf')
                    // ->updateFontSize(12);
                    $qrcode = (new QrCode($data->url))->useLogo(Yii::$app->basePath . '/web/member/img/logo.png')
                                                      ->setLogoWidth(30)
                                                      ->setSize(100)
                                                      ->setMargin(5)
                                                      ->useForegroundColor(51, 153, 255)
                                                      ->useBackgroundColor(200, 220, 210)
                                                      ->useEncoding('UTF-8')
                                                      ->setErrorCorrectionLevel(ErrorCorrectionLevelInterface::HIGH);
                                                    //   ->setLabel($label);
                    return Html::img($qrcode->writeDataUri());
                }
            ],
            [
                'attribute' => 'relation',
                'format' => 'raw',
                'value' => function($data) {
                    if (empty($data->relation)) {
                        return '<span class="not-set">(未设置)</span>';
                    } else {
                        $data = ['media_id' => $data->relation];
                        $re = Wechat::material_get($data);
                        return Html::a($re['news_item'][0]['title'], $re['news_item'][0]['url'], ['target' => '_blank']);
                    }
                }
            ],
            'description',            
            [
                'attribute' => 'type',
                'value' => function($data) {
                    switch ($data->type) {
                        case 1:
                            $v = '临时的整型';
                            break;
                        case 2:
                            $v = '临时的字符串';
                            break;
                        case 3:
                            $v = '永久的整型';
                            break;
                        case 4:
                            $v = '永久的字符串';
                            break;
                    }
                    return $v;
                }
            ],
            // 'scene_id',
            // 'scene_str',
            // 'expire_seconds',
            // 'ticket',
            // 'url:url',
            [
                'attribute' => 'created_at',
                'value' => function($data) {
                    return date('Y-m-d H:i:s', $data->created_at);
                }
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view}',
            ],
        ],
    ]); ?>
</div>
